<?php
class Main_labelApp extends MemberbaseApp
{
	var $_main_mod;
	
	function __construct(){
		parent::__construct();
		$this->Main_labelApp();
	}
	
	function Main_labelApp(){
		$this->_main_mod=& m('main_label');
	}
	
	function index(){
		$page=$this->_get_page();
		$main_list=$this->_main_mod->find(array(
			'join'=>'belong_to_brand',
			'order'=>'m_id desc',
			'limit'=>$page['limit']	,
			'count'=>true
		));
		$page['item_count'] = $this->_main_mod->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page);
		$this->assign('main_list',$main_list);
		$this->display('main_label.index.html');
	}
	
	
	function edit(){
		$m_id=isset($_GET['m_id'])?intval($_GET['m_id']):0;
		if (!$m_id){
			$this->show_warning('main_label_no_exist');
			return;
		}
		$file_mod =& m('upload');
		if (!IS_POST){
			$brand_mod=&m('brand');
			$brand_list=$brand_mod->find(array(
					'join'=>'belong_company',
					'conditions'=>'brand.if_show=1',
					'order'=>'brand.brand_id desc'
			));
			$this->assign('brand_list',$brand_list);
			$main=$this->_main_mod->get($m_id);
			if (empty($main)){
				$this->show_warning('main_label_no_exist');
				return;
			}
			$main_file=$file_mod->find(array(
				'conditions' => 'file_type = ' . BELONGS_MAIN_LABEL . ' AND key_id = ' . $m_id,
        		'order' => 'file_id ASC',	
			));
			$this->assign('main',$main);
			$this->assign('main_file',$main_file);
			$this->assign('belongs_main_label',BELONGS_MAIN_LABEL);
			$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('main_label.form.html');
		}else{
			$data=array();
			$main_code=trim($_POST['main_code']);
			if (!$main_code){
				$this->show_warning('main_code_empty');
				return;
			}
			$data['main_code']=$main_code;
			$data['brand_id']=isset($_POST['brand_id'])?intval($_POST['brand_id']):0;
			$this->_main_mod->edit($m_id,$data);
			if ($this->_main_mod->has_error()){
				$this->show_warning($this->_main_mod->get_error());
				return;
			}
			$this->show_message('edit_main_label_success',Lang::get('main_label_manage'),'index.php?app=main_label');
		}
	}
	
	
	function add(){
		if (!IS_POST){
			$brand_mod=&m('brand');
			$brand_list=$brand_mod->find(array(
					'join'=>'belong_company',
					'conditions'=>'brand.if_show=1',
					'order'=>'brand.brand_id desc'
			));
			$this->assign('brand_list',$brand_list);
			$this->assign('belongs_main_label',BELONGS_MAIN_LABEL);
			$this->assign('default_cust_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->display('main_label.form.html');
		}else{
			$data=array();
			$main_code=trim($_POST['main_code']);
			if (!$main_code){
				$this->show_warning('main_code_empty');
				return;
			}
			$data['main_code']=$main_code;
			$data['brand_id']=isset($_POST['brand_id'])?intval($_POST['brand_id']):0;
			$m_id=$this->_main_mod->add($data);
			if ($this->_main_mod->has_error()){
				$this->show_warning($this->_main_mod->get_error());
				return;
			}
			$main_label_id=isset($_POST['main_label_id'])?$_POST['main_label_id']:'';
			$file_mod =& m('upload');
			if(!empty($main_label_id))
			{
				$file_ids=implode(",",$main_label_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_MAIN_LABEL." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
				 
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $m_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$m_id,
							"session_id"=>session_id()
					));
				}
			}
			$this->show_message('add_main_label_success',Lang::get('main_label_manage'),'index.php?app=main_label');
		}
	}
	
	
	function drop(){
		$m_id=isset($_GET['m_id'])?intval($_GET['m_id']):0;
		if (!$m_id){
			$this->show_warning('main_label_no_exist');
			return;
		}
		$this->_main_mod->drop($m_id);
		if ($this->_main_mod->has_error()){
			$this->show_warning($this->_main_mod->get_error());
			return;
		}
		$this->show_message('drop_success');
	}
}